//
//  MYMYStatusCell.swift
//  Sina
//
//  Created by 王亮龙1 on 16/5/24.
//  Copyright © 2016年 lianglong. All rights reserved.
//

import UIKit
import SnapKit


class MYStatusCell: UITableViewCell {
    
    var toolBarTopCons: Constraint?
    
    var statusViewModel: MYStatusViewModel? {
        didSet {
            
            statusOriginalView.statusViewModel = statusViewModel
            toolBarView.statusViewModel = statusViewModel
            
            toolBarTopCons?.uninstall()
            
            if statusViewModel?.status?.retweeted_status == nil {
                //原创微博
                retweetedView.statusViewModel = nil
                retweetedView.hidden = true
                toolBarView.snp_updateConstraints(closure: { (make) in
                    self.toolBarTopCons = make.top.equalTo(statusOriginalView.snp_bottom).constraint
                })
            } else {
                //转发微博
                retweetedView.statusViewModel = statusViewModel
                retweetedView.hidden = false
                toolBarView.snp_updateConstraints(closure: { (make) in
                    self.toolBarTopCons = make.top.equalTo(retweetedView.snp_bottom).constraint
                })
            }
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        //self.backgroundColor = UIColor.brownColor()
        setupUI()
        //设置选中模式
        selectionStyle = .None
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    //布局子控件
    private func setupUI() {
        
        contentView.addSubview(statusOriginalView)
        contentView.addSubview(toolBarView)
        contentView.addSubview(retweetedView)
    
        statusOriginalView.snp_makeConstraints { (make) in
            make.top.left.right.equalTo(self.contentView)
        }
        
        retweetedView.snp_makeConstraints { (make) in
            make.top.equalTo(statusOriginalView.snp_bottom)
            make.left.right.equalTo(self.contentView)
        }
        
        toolBarView.snp_makeConstraints { (make) in
           self.toolBarTopCons = make.top.equalTo(retweetedView.snp_bottom).constraint
            make.left.right.equalTo(self.contentView)
            make.height.equalTo(36)
        }
        
        contentView.snp_makeConstraints { (make) in
            make.right.left.top.equalTo(self)
            make.bottom.equalTo(toolBarView.snp_bottom)
        }
    }
    
    
    
    //懒加载子视图
    private lazy var statusOriginalView: MYStatusOriginalView = MYStatusOriginalView()
    private lazy var toolBarView: MYStatusToolBarView = MYStatusToolBarView()
    private lazy var retweetedView: MYStatusretweetedView = MYStatusretweetedView()

    

}
